﻿/**
*
* Author: Zero
* Version: 1.0
* Clase que se encarga de relacionar las extensions y los movieClips de las extensions
* Version: 1.1
* le saqué toda la problemática de los filtros, porque eso lo tiene que hacer otra clase
* Version: 1.2
* Actualizo las luces de mensajes cuando hago un refresh de las extensions
*
*/

import com.dinmax.flash.utilities.movieclip.MovieClipUtils
import com.dinmax.flash.draw.cabeza.WindowCabeza
import com.dinmax.flash.central.draw.DepthManager
import com.dinmax.flash.common.collection.*
import com.dinmax.flash.central.bridge.*
import com.dinmax.flash.central.extension.*
import com.dinmax.flash.central.extension.log.*
import com.dinmax.flash.central.*

class ExtensionsToClips {
	
	private static var LINK_EXT:String="Extension";
	
	private var extensions:Extensions;
	private var activeExtensionID:Number;
	private var windowClips:WindowCabeza;
	private var extClips:Collection;
	
	private static var extensionsToClips:ExtensionsToClips;
	private static var rootClip:EXTENSIONBridge;
	
	public static function getRootClip():EXTENSIONBridge {
		//trace("ExtensionsToClips.as getRootClip() = "+rootClip);
		return rootClip;
	};

	public static function setRootClip(p_rootClip:EXTENSIONBridge):Void {
		//trace("ExtensionsToClips.as setRootClip() = "+p_rootClip);
		rootClip = p_rootClip;
		return;
	};
	
	public static function newInstance():Void {
		extensionsToClips= new ExtensionsToClips();
	}; //fin función
	
	public static function getActive():ExtensionsToClips {
		return extensionsToClips;
	}; //fin función
	
	function ExtensionsToClips (){
		extClips = new Collection();
	};
	
	public function setActiveExtensionID(id:Number):Void {
		this.activeExtensionID = id;
		return;
	};
	
	public function setExtensions(extensions:Extensions, activeExtensionID:Number, window:WindowCabeza, bridge:CENTRALBridge):Void {
		if(this.getExtensions() == undefined){
			this.extensions = extensions;
			this.activeExtensionID = activeExtensionID;
			this.createClips(window, bridge);
		} else {
			this.refreshClips(extensions);
		};
		return;
	};
	
	public function getExtensions():Extensions {
		return extensions;
	}
		
	public function getClips():Collection {
		return extClips;
	}
	
	public function createClips(window:WindowCabeza, bridge:CENTRALBridge):Void {
		windowClips = window;
		for(var i:Number=0; i < extensions.getItems().length; i++){
			var ext:Extension = extensions.getItems()[i];
			this.createClip(ext, bridge, true);
			
		};
		
	}
	
	public function createClip(ext:Extension, bridge:CENTRALBridge):Void {
		var xtId:String = String (ext.getId()); // paso la ID a string porque el key es un string
		if(this.checkActiveExtension(ext)){
			/*
			var clip:MovieClip = bridge.createRootExtension();
			trace("ExtensionsToClips.as createClip() clip= "+clip);
			trace("ExtensionsToClips.as createClip() clip= "+EXTENSIONBridge(clip));
			*/
			ExtensionsToClips.setRootClip(bridge.createRootExtension());
			var clip:MovieClip = ExtensionsToClips.getRootClip();
		} else {
			var clip:MovieClip = windowClips.drawItem();
		};
		EXTENSIONBridge(clip).setBridge(bridge);
		DepthManager.getActive().registerClip(clip);
		extClips.addItem(xtId, clip);
	}
	
	public function getExtension(clip:MovieClip):Extension {
		var xt:Extension;
		for(var i:Number = 0 ; i < extClips.getItems().length; i++){
			var xtClip:MovieClip = extClips.getItems()[i];
			if(xtClip.getValue() == clip){
				xt = extensions.getItem(xtClip.getKey());
			};
		};
		return xt;
	};
	
	public function getExtensionById(id:Number):Extension {
		var xt:Extension;
		for(var i:Number = 0 ; i < extensions.getItems().length; i++){
			if(extensions.getItems()[i].getId() == id){
				xt = extensions.getItems()[i];
			};
		};
		return xt;
	};

	public function getClipExtension(extension:Extension):MovieClip {
		
		var xtClip:MovieClip;
		for(var i:Number = 0 ; i < extensions.getItems().length; i++){
			var xt:Extension = extensions.getItems()[i];
			if(xt == extension){
				
				var xtId:String = String(extension.getId());
				xtClip = MovieClip(extClips.getItem(xtId).getValue());
				
			};
		};
		return xtClip;
	};
	
	public function refreshClips(extensions:Extensions):Void {
		//trace("ExtensionsToClips.as refreshClips()");
		for(var i:Number = 0; i < extensions.getItems().length; i++){
			var xt:Extension = extensions.getItems()[i];
			var xtId:String = String (xt.getId());
			if(this.extensions.getItem(xtId) != undefined){
				//LA EXTENSION YA EXISTE, SOLO LA ACTUALIZO
				//trace("ExtensionToClips.as | refreshClips() | la extension ya existe");
				
				var oldLog:ExtensionLog = this.extensions.getItem(xtId).getLog();
				var newLog:ExtensionLog = xt.getLog();
				oldLog.addItem(newLog.getItems()[0].getName(), newLog.getItems()[0]);
				xt.setLog(oldLog);
				
				this.extensions.removeItem(xtId);
				this.extensions.addItem(xtId, xt);
				this.extClips.getItem(xtId).getValue().onLoad();
				this.resetData();
				
			} else {
				//CREO LA EXTENSION
				this.extensions.addItem(xtId, xt);
				createClip(xt);
			}
		}
		
	}
	
	public function resetData():Void {
		for(var i:Number=0; i < extClips.getItems().length; i++){
			
			extClips.getItems()[i].getValue().resetData();
			extClips.getItems()[i].getValue().refreshButtonBar();
		};
	};
	
	public function isExtensionAutorized(ext:MovieClip):Boolean {
		return this.checkActiveExtension(this.getExtension(ext));
	};
	
	private function checkActiveExtension(ext:Extension):Boolean {
		return ext.getId() == this.activeExtensionID;
	};


}